package com.thinkgem.jeesite.modules.spider.pipeline;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.thinkgem.jeesite.modules.spider.common.SpiderConstants;
import com.thinkgem.jeesite.modules.spider.entity.SpiderLog;
import com.thinkgem.jeesite.modules.spider.entity.StackoverflowUser;
import com.thinkgem.jeesite.modules.spider.service.SpiderLogService;
import com.thinkgem.jeesite.modules.spider.service.StackoverflowUserService;

import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

@Component
public class StackoverflowUserPipeline implements Pipeline {
	private Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	private StackoverflowUserService stackoverflowUserService;
	@Autowired
	private SpiderLogService logService;

	@Override
	public void process(ResultItems resultItems, Task task) {
		StackoverflowUser user = resultItems.get("user");
		if (user != null) {
			try {
				stackoverflowUserService.save(user);
			} catch (Exception e) {
				logService.save(
						new SpiderLog("StackOverflowSpider", SpiderConstants.LOG_LEVEL_ERROR, e.getLocalizedMessage()));
				logger.error("insert user error !");
			}
			logger.debug("user==>" + user);
		}

	}

}
